package com.zjsru.oneDay202210;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: CookLee
 * @Date: 2022/10/29
 *  统计匹配检索规则的物品数量
 *  如果第 i 件物品能满足下述条件之一，则认为该物品与给定的检索规则 匹配 ：
 *
 * ruleKey == "type" 且 ruleValue == typei 。
 * ruleKey == "color" 且 ruleValue == colori 。
 * ruleKey == "name" 且 ruleValue == namei 。
 *
 * 输入：items = [["phone","blue","pixel"],["computer","silver","lenovo"],["phone","gold","iphone"]], ruleKey = "color", ruleValue = "silver"
 * 输出：1
 * 解释：只有一件物品匹配检索规则，这件物品是 ["computer","silver","lenovo"] 。
 *
 */
public class CountMatches {
    
    /**
     * 模拟
     */
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        int ans = 0;
        for (List<String> list : items) {
            if ((ruleKey.equals("type"))) {
                String s = list.get(0);
                if (s.equals(ruleValue)) {
                    ans++;
                }
            } else if (ruleKey.equals("color")) {
                String s = list.get(1);
                if (s.equals(ruleValue)) {
                    ans++;
                }
                
            } else if (ruleKey.equals("name")) {
                String s = list.get(2);
                if (s.equals(ruleValue)) {
                    ans++;
                }
            }
        }
        return ans;
    }
    
    /**
     * 模拟
     */
    public int countMatches2(List<List<String>> items, String k, String v) {
        //判断所属类型的开头第一个字母
        int ans = 0, idx = k.charAt(0) == 't' ? 0 : k.charAt(0) == 'c' ? 1 : 2;
        for (List<String> item : items) {
            if (item.get(idx).equals(v)) {
                ans++;
            }
        }
        return ans;
    }
    
    public static void main(String[] args) {
        CountMatches countMatches = new CountMatches();
        List<List<String>> items = new ArrayList<>();
        //测试用例
        List<String> list1 = new ArrayList<>();
        list1.add("phone");
        list1.add("blue");
        list1.add("pixel");
        items.add(list1);
        List<String> list2 = new ArrayList<>();
        list2.add("computer");
        list2.add("silver");
        list2.add("phone");
        items.add(list2);
        List<String> list3 = new ArrayList<>();
        list3.add("phone");
        list3.add("gold");
        list3.add("iphone");
        items.add(list3);
        String ruleKey = "type";
        String valueKey = "phone";
        System.out.println(countMatches.countMatches(items, ruleKey, valueKey));
    }
}
